#include "AntWorld.h"
#include "FoodSource.h"
#include "Game.h"
#include "GroundPlane.h"
#include "WorkerAnt.h"
#include "AntHill.h"
#include "Predator.h"
#include "ControllablePredator.h"

#include <assert.h>

AntWorld::AntWorld() : m_currentIDCount(0)
{
	
	
	
	m_pGround = new GroundPlane(80, "BarrenEarth.jpg", m_currentIDCount);
	TheGame::Instance()->AddGameObject(m_currentIDCount, m_pGround);
	//TheGame::Instance()->AddGameObject(new GroundPlane(20, "BarrenEarth.jpg"));
	m_currentIDCount++;

	
	m_pAntHill = new AntHill(0.8f);
	TheGame::Instance()->AddGameObject(m_currentIDCount, m_pAntHill);
	m_currentIDCount++;
	
	/*
	FoodSource* food = new FoodSource(Vec3f(-8,2,-8), "Leaf.jpg", m_currentIDCount);
	TheGame::Instance()->AddGameObject(m_currentIDCount, food);
	m_food.push_back(food);
	m_currentIDCount++;
	
	FoodSource* food1 = new FoodSource(Vec3f(8,2,-8), "Leaf.jpg", m_currentIDCount);
	TheGame::Instance()->AddGameObject(m_currentIDCount, food1);
	m_food.push_back(food1);
	m_currentIDCount++;
	*/

	Predator* pred = new Predator(Vec3f(-10, 2, -10), m_currentIDCount); 
	TheGame::Instance()->AddGameObject(m_currentIDCount, pred);
	m_pPredator = pred;
	m_currentIDCount++;

	ControllablePredator* controlPred = new ControllablePredator(Vec3f(15, 2, -15), m_currentIDCount);
	TheGame::Instance()->AddGameObject(m_currentIDCount, controlPred);
	m_pControlledPredator = controlPred;
	m_currentIDCount++;

	CreateNewFood();
	CreateNewFood();
	CreateNewFood();
	CreateNewFood();
	CreateNewFood();

	int i = 0;
	for( ; i < 15; i++)
	{
		WorkerAnt* worker = new WorkerAnt(Vec3f(0, 2, 0), 15.0f, m_currentIDCount+i);
		m_agentGroup.AddAgent(worker);
		m_ants.push_back(worker);
	}
	m_currentIDCount += i;
	m_currentIDCount++;
	
}

void AntWorld::Update()
{

}

void AntWorld::CreateNewFood()
{
	int minDist = 30;
	int maxDist = 50;

	int xPos = rand() % maxDist - minDist;
	int zPos = rand() % maxDist - minDist;

	/*
	float xRand = (float)((rand() % 20 -10 / 10));
	float zRand = (float)((rand() % 20 -10 / 10));

	float x = xPos * xRand;
	float z = zPos * zRand;
	*/

	FoodSource* food = new FoodSource(Vec3f((float)xPos,2.0f,(float)zPos), "Leaf.jpg", m_currentIDCount);
	TheGame::Instance()->AddGameObject(m_currentIDCount, food);
	m_food.push_back(food);
	m_currentIDCount++;
	
}

FoodSource* AntWorld::FoodInRange(Vec3f pos, float radius)
{
	std::vector<FoodSource*>::iterator it = m_food.begin();
	float radSqr = radius * radius;
	float minDistSqr = radSqr;

	FoodSource* result = 0;

	for( ; it != m_food.end(); ++it)
	{
		float distSqr = ((*it)->GetPos() - pos).LengthSqr();
		//If food is in the radius
		if( distSqr < radSqr)
		{
			//and if the food is the closest
			if( distSqr < minDistSqr)
			{
				minDistSqr = distSqr;
				result = *it;
			}
		}		
	}

	return result;
}

void AntWorld::ClearFood(int id)
{
	std::vector<FoodSource*>::iterator it = m_food.begin();
	std::vector<FoodSource*>::iterator end = m_food.end();

	for ( ; it != end; ++it)
	{
		if( (*it)->GetID() == id)
		{
			m_food.erase(it);
			return;
		}
	}
	assert(0);
}

Predator* AntWorld::GetRandPredator()
{
	return m_pPredator;
}
ControllablePredator* AntWorld::GetControlledPredator()
{
	return m_pControlledPredator;
}

AntHill* AntWorld::GetAntHill()
{
	return m_pAntHill;
}
